package com.pi.pro.xcmg.am.was.checker;


import com.pi.common.CodesConstants;
import com.pi.config.SapWmsClientExchangeConfig;
import com.pi.framework.core.util.DateUtils;
import com.pi.framework.core.util.SecurityUtils;
import com.pi.framework.core.util.StringUtils;
import com.pi.framework.fac.MessageResult;
import com.pi.framework.fac.MessageResultFactory;
import com.pi.pro.xcmg.am.common.XcmgAmConstants;
import com.pi.pro.xcmg.am.was.edi.mes.service.MasterDataServiceProxy;
import com.pi.pro.xcmg.am.was.edi.sap.common.SapWasXcmgExchangeConstants;
import com.pi.pro.xcmg.am.was.edi.sap.common.SapWasXcmgExchangeSupport;
import com.pi.pro.xcmg.am.was.edi.sap.dto.reply.DeliveryNoteData;
import com.pi.pro.xcmg.am.was.edi.sap.dto.reply.DeliveryNoteReplyData;
import com.pi.pro.xcmg.am.was.edi.sap.dto.req.DeliveryNoteQueryData;
import com.pi.pro.xcmg.am.was.fac.service.SapCommonWorkService;
import com.pi.pro.xcmg.am.was.fac.service.WasCustomWorkService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


import java.util.*;


/**
 * 功能:
 * ---------------------------------------
 * 1. 定时从SAP获取自制件出库单
 */
@Slf4j
@Component
public class SapWasDeliveryNoteLoadBeatChecker {

    @Autowired
    private SapCommonWorkService sapCommonWorkService;

    @Autowired
    private WasCustomWorkService wasCustomWorkService;

    @Autowired
    private SapWmsClientExchangeConfig clientExchangeConfig;

    @Autowired
    private MasterDataServiceProxy masterDataServiceProxy;

    //
    private Date lastCheckDate=null;

    //
    public MessageResult<?> perform(){
        String m="";

        this.masterDataServiceProxy.loadMaterials();
        if ((this.clientExchangeConfig!=null)
                && (!this.clientExchangeConfig.interfaceActive())){
            return MessageResultFactory.okResult();
        }
        //
        DeliveryNoteReplyData replyData=null;
        List<DeliveryNoteData> dns=null;
        DeliveryNoteQueryData queryData=new DeliveryNoteQueryData();
        Date lastdate = this.lastCheckDate;
        if (lastdate==null){
            lastdate = DateUtils.add(new Date(), Calendar.DATE, -1);
        }
        String divisionCode=XcmgAmConstants.DIVISION_WH101_CODE;
        try{
            queryData.setIwerks(SapWasXcmgExchangeConstants.PLANT_CODE_XCMG_AE);
            queryData.setIpstmp(DateUtils.format(lastdate, DateUtils.FORMAT_DATE_YYYY_MM_DD_HHMMSS));
            replyData=this.sapCommonWorkService.loadDeliveryNotesData(divisionCode, queryData);
            m = SapWasXcmgExchangeSupport.check(replyData);
            if (!StringUtils.isNullOrBlank(m)){
                return MessageResultFactory.errorResult(m);
            }
            if (replyData.getZpszl()==null){
                return MessageResultFactory.okResult();
            }
            dns=replyData.getZpszl().getItem();
            if ((dns==null)||(dns.size()==0)){
                return MessageResultFactory.okResult();
            }
            m=this.wasCustomWorkService.loadSelfMadeSoDocuemnts(divisionCode, dns, SecurityUtils.getLoginUsername());
            if (!StringUtils.isNullOrBlank(m)){
                return MessageResultFactory.errorResult(m);
            }
            //TODO:HU/202506
            //save to DB
            lastdate=SapWasXcmgExchangeSupport.lastCheck(replyData);
            if (lastdate!=null){
                lastCheckDate=lastdate;
            }
        }catch (Exception e){
            log.error("Job SapWasDeliveryNoteLoadBeatChecker WORK/performm() error//{error:{}}",e.getMessage());
        }
        return MessageResultFactory.okResult();
    }

}
